//数组里面的数字从大到小排序 必须是数字
function paixuMax(n) {
    n.sort(function (a, b) { return b - a })
}
//数组里面的数字从小到大排序 必须是数字
function paixuMin(n) {
    n.sort(function (a, b) { return a - b })
}
//数组里面最大的值
function myArrayMax(arr) {
    return Math.max.apply(null, arr);
}
//冒泡排序从小到大
function bubbleSort(arr) {
    for (var i = 0; i < arr.length - 1; i++) {
        var isSort = true
        for (var j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                isSort = false
                var temp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = temp
            }
        }
        if (isSort) {
            break
        }
    } return arr
}
//选择排序从小大大
function selectSort(arr) {
    for (var i = 0; i < arr.length; i++) {
        var min = arr[i]
        var minIndex = i
        for (var j = i + 1; j < arr.length; j++) {
            if (arr[j] < min) {
                min = arr[j]
                minIndex = j
            }
        }
        var temp = arr[minIndex]
        arr[minIndex] = arr[i]
        arr[i] = temp
    }
    return arr
}
//数组去重
function norepeat(arr) {
    var newarr = [];
    for (var i in arr) {
        if (newarr.indexOf(arr[i]) == -1) {
            newarr.push(arr[i]);
        }
    }
    return newarr;
}
//统计字符串出现的次数
function strRepeat(str) {
    var arr = str.split("")
    var strNew = []

    var countStr = []//存放重复次数
    for (var i = 0; i < arr.length; i++) {

        if (strNew.indexOf(arr[i]) == -1) {
            strNew.push(arr[i])
            var count = 1 //计算重复次数
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    count++
                }
            }
            countStr.push(count)
        }
        arr.shift()
        i--
    }

    for (var i = 0; i < strNew.length; i++) {  //拼接
        strNew[i] += countStr[i]
    }
    console.log(strNew.join(','))
}
//获取两个数之间的随机数
function getRandomNum(min, max) {
    var t = parseInt(Math.random() * (max - min))
    return t + min
}
//根据时间判断今天星期几 ,返回值是汉字(一二三四五六日)
function weekDay(t) {
    var hanzi = ['日', '一', '二', '三', '四', '五', '六']
    var i = t.getDay()
    return hanzi[i]
}
//生成(返回值)一个随机的16进制颜色  例如#ffffff
function randomColor() {
    var t = Math.random().toString(16)
    t = t.substr(2, 6)
    return "#" + t
}
// 简化查找标签化
function $(id) {
    if (id.charAt(0) == '#') {
        return document.getElementById(id.substring(1))
    }
    else if (id.charAt(0) == '.') {
        return document.getElementsByClassName(id.substring(1))
    } else {
        return document.getElementsByTagName(id)
    }

}
//获取dom元素样式属性 比如写在style里面的就不能直接获取
function getStyle(dom,attribute){
    if(dom.currentStyle){
        return dom.currentStyle[attribute]     //不写.,是因为attribute是变量
    }else{
        return window.getComputedStyle(dom)[attribute];
    }
}
//拖拽
/*
功能:拖拽
参数: 
moveDom:移动的dom
dragDom:触发拖拽的dom (dragDom是moveDom的子元素,比如窗口按住导航栏拖拽,整个窗口会移动):  可选
parentDom:父元素(移动的的区域)可以限制只在哪块区域内移动:  可选
返回值:无
*/
function drag(moveDom,dragDom,parentDom){
    parentDom = parentDom||moveDom.offsetParent //如果有就有,如果没有就是||后面的,offsetPartDom找距离他设置过定位的父元素
    dragDom =dragDom||moveDom //没有的话,就是移动dom本身
    var offsetX
    var offsetY
    var moveFn = function(event){
        var e = event||window.event
        //计算 盒子的left 和 top
        var left1 =e.pageX - offsetX-parentDom.offsetLeft
        var top1 =e.pageY - offsetY-parentDom.offsetTop
        if(left1<0){
            left1=0
        }else if(left1>(parentDom.offsetWidth - moveDom.offsetWidth)){
            left1= parentDom.offsetWidth-moveDom.offsetWidth
        }
        if(top1<0){
            top1=0
        }else if (top1>(parentDom.offsetHeight - moveDom.offsetHeight)){
            top1 = parentDom.offsetHeight - moveDom.offsetHeight;
        }
        //改变盒子的left和top
        moveDom.style.left = left1+'px'
        moveDom.style.top = top1+'px'
    }
    dragDom.addEventListener('mousedown',mousedownFn)
    function mousedownFn(event){
        var e =event||window.event
        offsetX=e.offsetX
        offsetY = e.offsetY
        parentDom.addEventListener('mousemove',moveFn)
    }
    document.addEventListener('mouseup',()=>{
        parentDom.removeEventListener('mousemove',moveFn)
    })
    

}
//拖拽可以按轨迹返回
function dragBack(moveDom,dragDom,parentDom,isBackPlay){
    parentDom = parentDom||moveDom.offsetParent //如果有就有,如果没有就是||后面的,offsetPartDom找距离他设置过定位的父元素
    dragDom =dragDom||moveDom //没有的话,就是移动dom本身
    var offsetX
    var offsetY
    var historys=[]
    var moveFn = function(event){
        var e = event||window.event
        //计算 盒子的left 和 top
        var left1 =e.pageX - offsetX-parentDom.offsetLeft
        var top1 =e.pageY - offsetY-parentDom.offsetTop
        if(left1<0){
            left1=0
        }else if(left1>(parentDom.offsetWidth - moveDom.offsetWidth)){
            left1= parentDom.offsetWidth-moveDom.offsetWidth
        }
        if(top1<0){
            top1=0
        }else if (top1>(parentDom.offsetHeight - moveDom.offsetHeight)){
            top1 = parentDom.offsetHeight - moveDom.offsetHeight;
        }
        isBackPlay&&historys.push({
            left:left1,
            top:top1
        })
        //改变盒子的left和top
        moveDom.style.left = left1+'px'
        moveDom.style.top = top1+'px'
    }
    dragDom.addEventListener('mousedown',mousedownFn)
    function mousedownFn(event){
        var e =event||window.event
        offsetX=e.offsetX
        offsetY = e.offsetY
        parentDom.addEventListener('mousemove',moveFn)
    }
    document.addEventListener('mouseup',function(){
        parentDom.removeEventListener('mousemove',moveFn);
       
        (isBackPlay && historys.length>0) && backPlay();
    })
    function backPlay(){
        var index = historys.length-1
        
        var myTimer =setInterval(function(){
            moveDom.style.left =historys[index].left+'px'
            moveDom.style .top= historys[index].top+'px'
            index--
            if(index<0){
                clearInterval(myTimer)
                historys.length = 0
            }
        },10) 
    }

}
//封装正则
function regExpCheck(type,str){

    var regObj = {
        "mail":/^\w{4,25}@\w{2,10}\.(com|net|cn)$/,
        "date":/^((\d{4}\-\d{2}\-\d{2})|(\d{4}\/\d{2}\/\d{2})|(\d{4}\.\d{2}\.\d{2}))$/i,
       
        'username':/^[a-zA-Z_]\w{5,15}$/i,
        'password':/^(?!\d+$)(?![a-z]+$)[a-z\d]{6,16}$/i,
        'myname':/^[\u4e00-\u9fa5]{2,10}$/i,
        'idcard':/^[1-8]\d{16}[X\d]$/i,
        'phone':/^1\d{10}$/,
        "ip":/^([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$/
    }

    return regObj[type]?regObj[type].test(str):false;
}
//敏感词过滤
function mingan(str){
  
    
    return str.replace(/(sb|cao|操)/gi,'**');
}